package com.sky.service.admin.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.CategoryDTO;
import com.sky.exception.BaseException;
import com.sky.mapper.admin.CategoryMapper;
import com.sky.mapper.admin.DishMapper;
import com.sky.mapper.admin.SetmealDishMapper;
import com.sky.pojo.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.admin.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class CategoryServiceImpl  implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealDishMapper setmealMapper;
    /**
     * 添加分类
     * @param categoryDTO
     */
    @Override
    public void addCategory(CategoryDTO categoryDTO) {
        Category category=new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        /*为分类设置一些默认参数*/
        category.setStatus(0);
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        /*关于创建人修改人id的暂时先设置为默认值*/
        category.setCreateUser(10L);
        category.setUpdateUser(10L);
        categoryMapper.addCategory(category);



    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @param type
     * @return
     */
    @Override
    public PageResult pageQuery(Integer page, Integer pageSize, String name, Integer type) {
        PageHelper.startPage(page,pageSize);
        /*下面写的sql不用去写分页2个参数了*/
        List<Category> categories = categoryMapper.pageQuery(name, type);
        /*将我们查到的集合向下转型*/
        Page<Category> pageInfo = (Page<Category>) categories;
        return new PageResult(pageInfo.getTotal(),pageInfo.getResult());
    }

    /**
     * 删除分类
     * @param id
     */

    @Override
    public void deleteById(Long id) {
        //查询菜品分类下是否有菜品
        Integer count = dishMapper.countByCategoryId(id);
        if (count>0){
            throw new BaseException("该分类下有菜品，不能删除");
        }
        //查询套餐分类下是否有套餐
        Integer count1 = setmealMapper.countByCategoryId(id);
        if (count1>0){
            throw new BaseException("该分类下有套餐 ，不能删除");
        }
        categoryMapper.deleteById(id);

    }

    @Override
    public void update(CategoryDTO categoryDTO) {
        Category category=new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        category.setUpdateTime(LocalDateTime.now());
        /*修改人的id就先不去弄了暂时是默认的*/
        categoryMapper.update(category);

    }

    /**
     * 启动或开启
     * @param id
     * @param status
     */

    @Override
    public void startOrstop(Long id, Integer status) {
        categoryMapper.startOrstop(id,status);

    }
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询分类
     * @param type
     * @return
     */
    @Override
    public List<Category> findList(Integer type) {
       /*添加redis缓存*/
        /*从redis里面获得数据
        *其实redisTemplate是帮我们序列化数据的,把查到的集合对话以json的形式存到了redis里面
        * 然后再从redis里面取数据,直接反序列化成集合,跟@ResponseBody一样,会把我们的集合封装为json返回给前端
        * */
        List<Category> list= (List<Category>) redisTemplate.opsForValue().get("category:" + type);
        if (list==null || list.size()==0){
            list=categoryMapper.findList(type);
            System.out.println("从数据库查询");
            redisTemplate.opsForValue().set("category:" + type,list);
        }


        return list;
    }
    /**
     * 根据类型查询分类
     * @param type
     * @return
     */

    public List<Category> list(Integer type) {
        //  添加redis缓存
        List<Category> list =  (List<Category>)redisTemplate.opsForValue().get("category:"+type);
        if(list==null||list.size()==0) {
            log.info("缓存中没有数据,从数据库里面查询");
            System.out.println("缓存中没有数据,从数据库里面查询");
            list = categoryMapper.list(type);
            redisTemplate.opsForValue().set("category:",list);
        }
        log.info("缓存中有数据,从缓存里面查询");
        System.out.println("缓存中有数据,从缓存里面查询");
        return list;
    }

}
